package net.xdclass.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ai")
public class PromptController {

    @Autowired
    private OpenAiChatModel openAiChatModel;

    @GetMapping("/prompt")
    public String prompt() {

        Prompt prompt = new Prompt("你的名称是");

        //结构化消息
        List<Message> list = List.of(
            new SystemMessage("你是小滴课堂的技术专家 冰冰"),
                new UserMessage("你们有什么课程"));

        Prompt promptList = new Prompt( list);

        ChatClient chatClient = ChatClient.builder(openAiChatModel).build();
        String content = chatClient.prompt(promptList).call().content();

        return content;
    }

    @GetMapping("/role")
    public String role() {
        ChatClient chatClient = ChatClient.builder(openAiChatModel).build();
        String content = chatClient.prompt().system("你是小滴课堂的技术专家 冰冰").user("你们有什么课程")
                .call().content();

        return content;
    }

    @GetMapping("/template")
    public String template(@RequestParam("msg") String msg) {
        //定义一个模板
        PromptTemplate template = new PromptTemplate("请以{msg}为主题，写一首诗");

        Prompt prompt = template.create(Map.of("msg", msg));

        ChatClient chatClient = ChatClient.builder(openAiChatModel).build();
        String content = chatClient.prompt(prompt).call().content();
        return content;
    }
}
